package com.xigua._链表;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: 定义一个函数，输入一个链表的头节点，反转该链表并输出反转后链表的头节点。
 * @date 2022/3/317:36
 */
public class _剑指Offer24_反转链表 {

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2,new ListNode(3,new ListNode(4)));
        ListNode listNode = reverseList(head);
        System.out.println(listNode);
    }

    public static ListNode reverseList(ListNode head) {
        ListNode newHead =null;
        while (head != null){
          ListNode tmp = head.next;
          head.next = newHead;
          newHead = head;
          head = tmp;
        }
        return newHead;
    }

}
